package testGuava.example.testGuava;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.google.common.primitives.Ints;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName : org.example.testGuava.Colltest
 * @Description : 类描述
 * Created by cyd on 2021-09-15 15:28:08
 */
public class CollTest {

    /**
     * Maps，Sets的用法一样
     * 用于简化构造集合的操作
     */
    @Test
    public void testLists() {
        ArrayList<String> list = Lists.newArrayList("1", "2", "3");
        System.out.println(list);

        ArrayList<Student> students = Lists.newArrayList(new Student("lili"), new Student("mike"));
        System.out.println(students);

        // 将list拆分
        List<List<String>> partition = Lists.partition(list, 2);
        partition.forEach(System.out::println);
    }

    /**
     * Ints,Longs
     * 快捷生成list
     */
    @Test
    public void testInts() {
        List<Integer> list = Ints.asList(1, 2, 3, 4, 5, 6);
        System.out.println(list);
    }

    /**
     * 不可变的集合
     */
    @Test
    public void testImmutableList() {

        ArrayList<Student> students = Lists.newArrayList(new Student("lili"), new Student("mike"));
        ImmutableList<Student> immutableList = ImmutableList.copyOf(students);

        students.remove(0);

        // 原集合改变了，不可变集合不会跟着变，这和jdk提供的不可变集合不同
        System.out.println(immutableList);
        System.out.println(students);

        // 另一种构造不可变集合形式
        ImmutableList<Object> build = ImmutableList.builder().
                add("123").
                add("456").
                addAll(students).
                build();
        System.out.println(build);
    }

}

class Student {
    private String studentName;

    public String getStudentName() {
        return studentName;
    }

    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }

    public Student(String studentName) {
        this.studentName = studentName;
    }

    @Override
    public String toString() {
        return "Student{" +
                "studentName='" + studentName + '\'' +
                '}';
    }
}
